package com.sanjiahantang.ythshopping.adapter.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.activity.me.JsWebActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.BaseModel;
import com.sanjiahantang.ythshopping.model.NearShop;
import com.sanjiahantang.ythshopping.presenter.CheckPresenter;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.CheckView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/7/30.
 */

public class NearByHolder extends MyBaseViewHolder implements CheckView{
    private Context context;

    private TextView tv_type, tv_more;
    private MyGridView gridView;

    private List<NearShop> datas;
    private CheckPresenter checkPresenter;


    SHopAdapter adapter;

    public NearByHolder(final Context context, View itemView, final List<NearShop> datas) {
        super(itemView);
        this.context=context;
        this.datas=datas;


        if (datas != null && datas.size() > 0){
            itemView.setVisibility(View.VISIBLE);
        }else{
            itemView.setVisibility(View.GONE);
            this.datas = new ArrayList<>();
        }
        tv_type = (TextView) itemView.findViewById(R.id.tv_type);
        tv_more = (TextView) itemView.findViewById(R.id.tv_more);
        gridView = (MyGridView) itemView.findViewById(R.id.gridview);
        checkPresenter = new CheckPresenter(this);
        gridView.setNumColumns(1);
//        gridView.setHorizontalSpacing(0);
//        gridView.setVerticalSpacing(0);
        tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, JsWebActivity.class);
                context.startActivity(intent);
            }
        });
        gridView.setVisibility(View.GONE);
        gridView.setRecyclerListener(new AbsListView.RecyclerListener() {
            @Override
            public void onMovedToScrapHeap(View view) {
                ImageView iv = (ImageView) view.findViewById(R.id.iv_nearby_pic);
                Utils.releaseImageViewResouce(iv);
            }
        });
    }

    @Override
    void bindData() {

        tv_type.setText("附近店铺");
//        adapter=new SHopAdapter(datas);
//        gridView.setAdapter(adapter);
    }

    @Override
    public void getData(BaseModel response, String url) {

        if (response.getCode() == null)
            return;
        if (Integer.parseInt(response.getCode()) < 0){
            ConfigValue.DATA_KEY = "";
        }

        Intent intent = new Intent(context, JsWebActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("url",url );
        intent.putExtras(bundle);
        ((Activity)context).startActivityForResult(intent, MainActivity.RESULT_NEAR);

    }

    public class SHopAdapter extends BaseAdapter {

        List<NearShop> data;
        LayoutInflater inflater;
        Picasso picasso;

        public SHopAdapter(List<NearShop> data){
            this.data=data;
            inflater=LayoutInflater.from(context);
            picasso = Picasso.with(context);
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHodle viewhodle;
            if (view==null)
            {
                view=inflater.inflate(R.layout.item_neaby_long,null);
                viewhodle=new ViewHodle();
                viewhodle.iv_pic= (ImageView) view.findViewById(R.id.iv_nearby_pic);
                viewhodle.tv_name= (TextView) view.findViewById(R.id.nearby_shop_name);
                viewhodle.tv_nearby_address = (TextView) view.findViewById(R.id.tv_nearby_address);
                viewhodle.eb_nearby_ = (RatingBar) view.findViewById(R.id.eb_nearby_);
                viewhodle.tv_nearby_dis = (TextView) view.findViewById(R.id.tv_nearby_dis);
                view.setTag(viewhodle);
            }else {
                viewhodle= (ViewHodle) view.getTag();
            }
            if (data.size() > 0){

                viewhodle.tv_name.setText(data.get(i).getShop_name());
                picasso.load(ConfigValue.IMG_IP + data.get(i).getShop_img()).into(viewhodle.iv_pic);
                viewhodle.iv_pic.setTag(R.id.decode,ConfigValue.IMG_IP + data.get(i).getShop_img());
                viewhodle.tv_nearby_address.setText(data.get(i).getShop_addr());




                viewhodle.tv_nearby_dis.setText(data.get(i).getShop_far() + "km");
                viewhodle.eb_nearby_.setRating(Integer.parseInt(data.get(i).getShop_level()));

                view.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        checkPresenter.checkKey(context,datas.get(i).getShop_url());
                    }
                });

            }


            return view;
        }

        public class ViewHodle
        {
            TextView tv_name;
            ImageView iv_pic;
            TextView tv_nearby_address;
            TextView tv_nearby_dis;
            RatingBar eb_nearby_;
        }
    }

    public static class ItemHolder extends MyBaseViewHolder implements CheckView{
        TextView tv_name;
        ImageView iv_pic;
        TextView tv_nearby_address;
        TextView tv_nearby_dis;
        RatingBar eb_nearby_;
        View view;
        NearShop data;
        Context context;
        Picasso picasso;
        CheckPresenter checkPresenter;
        public ItemHolder(View itemView,NearShop data,Context context) {
            super(itemView);
            this.data = data;
            this.context = context;
            picasso = Picasso.with(context);
            view = itemView;
            iv_pic= (ImageView) itemView.findViewById(R.id.iv_nearby_pic);
            tv_name= (TextView) itemView.findViewById(R.id.nearby_shop_name);
            tv_nearby_address = (TextView) itemView.findViewById(R.id.tv_nearby_address);
            eb_nearby_ = (RatingBar) itemView.findViewById(R.id.eb_nearby_);
            tv_nearby_dis = (TextView) itemView.findViewById(R.id.tv_nearby_dis);
            checkPresenter = new CheckPresenter(this);
        }

        @Override
        void bindData() {
            tv_name.setText(data.getShop_name());
            picasso.load(ConfigValue.IMG_IP + data.getShop_img())
                    .resize(Utils.dip2px(context,ConfigValue.IMAGE_SCALE),Utils.dip2px(context,ConfigValue.IMAGE_SCALE))
                    .centerCrop().into(iv_pic);
            iv_pic.setTag(R.id.decode,ConfigValue.IMG_IP + data.getShop_img());
            tv_nearby_address.setText(data.getShop_addr());


            tv_nearby_dis.setText(data.getShop_far() + "km");
            eb_nearby_.setRating(Integer.parseInt(data.getShop_level()));

            view.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    checkPresenter.checkKey(context,data.getShop_url());
                }
            });
        }

        @Override
        public void getData(BaseModel response, String url) {
            if (response.getCode() == null)
                return;
            if (Integer.parseInt(response.getCode()) < 0){
                ConfigValue.DATA_KEY = "";
            }

            Intent intent = new Intent(context, JsWebActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("url",url );
            intent.putExtras(bundle);
            ((Activity)context).startActivityForResult(intent, MainActivity.RESULT_NEAR);
        }
    }
}
